package org.azj.inireader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.DuplicateFormatFlagsException;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class BasicIniReader {

	// 用于存储结果的HASH表，key 是section
	protected HashMap<String, HashMap<String, String>> infoContainer = 
			new LinkedHashMap<String, HashMap<String, String>>();

	private HashMap<String, String> hash = null;
	private String fileName = null;
	
	public BasicIniReader() {}
	
	/*
	* Description: read the data from file.
	* Parameter: the file name.
	* Return: the read line number. return zero if file not found or empty. 
	*/
	public int read(String argFileName) throws 
		FileNotFoundException, IniFormatException, IOException
	{
	   int read_line = 0;
	   if (fileName != null) {
	     return read_line;
	   }
	   fileName = argFileName;
	   try {
		   BufferedReader reader = 
		       new BufferedReader(
				   new InputStreamReader(
				   new FileInputStream(argFileName),"GBK"));
	     String line;
	     while ((line = reader.readLine()) != null) {
	       read_line++;
	       parseLine(line);
	     }
	     reader.close();
	   } 
	   catch (FileNotFoundException e) {   
	       e.printStackTrace();   
	       // TODO:log
	       //Log.e("EncryptBox", "INIReader: read error.");
	       throw e;
	   } 
	   catch (IniFormatException e) {
		   e.printStackTrace();   
		   // TODO:log
	       //Log.e("EncryptBox", "INIReader: read error.");
		   throw e;
	   }
	   catch (IllegalArgumentException e) {
		   e.printStackTrace();   
		   // TODO:log
	       //Log.e("EncryptBox", "INIReader: read error.");
		   throw e;
	   }
	   catch (UnsupportedEncodingException e) {
		   e.printStackTrace();
		   // TODO:log
	       //Log.e("EncryptBox", "INIReader: read error.");
		   
		   throw e;
	   } 
	   catch (IOException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		   //TODO:log
		   //Log.e("EncryptBox", "INIReader: read error.");
		   
		   throw e;
	   }
	   
	   return read_line;
	}
	
	/*
	* Description: parse one line which read from file.
	* Parameter: the line.
	* Return: NONE. 
	*/
	protected void parseLine(String line) 
			throws IniFormatException, IllegalArgumentException, DuplicateFormatFlagsException
	{
		if (line.startsWith(";") == true)
		{
			// if this is a comment, pass it
			return;
		}
		
		String currentSectionName = null;
				
		// deal with each line
		line = line.trim();
		
		int beginBraceIndex = line.indexOf("[");
		int endBraceIndex = line.indexOf("]");
	    
		if (beginBraceIndex == 0 && endBraceIndex == line.length() - 1) 
	    {
			// a new table
	    	currentSectionName = line.substring(beginBraceIndex + 1, endBraceIndex);
	    	hash = new HashMap<String, String>();
	    	infoContainer.put(currentSectionName, hash);
	    } 
	    else if (line.matches(".*=.*")) 
	    {
	    	int i = line.indexOf('=');
	    	String name = line.substring(0, i).trim();
	    	String value = line.substring(i + 1).trim();
	    	
		    // 插入结果集
	    	hash.put(name, value);
	   }
	}
	
	/*
	* Description: get all sections' info
	*/
	public HashMap<String, HashMap<String, String>> getSectionInfo() {
	   if (fileName == null) {
		   return null;
	   }
	   
	   return  infoContainer;
	}
	
	/*
	* Description: get value by key
	*/
	public HashMap<String, String> getSectionInfo(String section) {
	   if (fileName == null) {
		   return null;
	   }
	   
	   return infoContainer.get(section);
	}
	
	/*
	* Description: get value by key
	*/
	public String getValueBySectionKey(String section, String key) {
	   if (fileName == null) {
		   return null;
	   }
	   
	   return infoContainer.get(section).get(key);
	}
}
